package com.lxm.ymcc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.lxm.ymcc.domain.CourseMarket;
import com.lxm.ymcc.domain.CourseUserLearn;
import com.lxm.ymcc.dto.PayResultVo;
import com.lxm.ymcc.mapper.CourseUserLearnMapper;
import com.lxm.ymcc.service.ICourseMarketService;
import com.lxm.ymcc.service.ICourseUserLearnService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-10-23
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseMarketService courseMarketService;
    @Override
    public void addCourseLearn(PayResultVo payResultVo) {
         /*
            Map<String,Object> map  = new HashMap<>();
            map.put("userId",loginId);
            map.put("courseIds",ids);
            orderSn:"35165416"
            extParams:{"userId":3,"courseIds":"9,3"}
         */
        String extParams = payResultVo.getExtParams();
        Map<String,Object> map = JSON.parseObject(extParams, Map.class);
        Long userId = Long.valueOf(map.get("userId").toString());
        //["9","3"]
        String[] ids = map.get("courseIds").toString().split(",");
        /*
            1.根据id-用户 和课程去数据库查询 是否已经购买过此课程
                没有：新生成
                买过：更新过期时间  找到课程market里面的有效天数，从当前时间 推迟 n天
                   没过期：结束时间 + 有效天数
                   过期：当前时间+有效天数
         */
        for (String id : ids) {
            //查询用户是否购买过此课程
            Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
            wrapper.eq("login_id",userId);
            wrapper.eq("course_id",id);
            CourseUserLearn courseUserLearn = selectOne(wrapper);
            //查询用户购买课程有效期+
            CourseMarket courseMarket = courseMarketService.selectById(id);
            //计算当前课程的结束时间
            Date date = DateUtils.addDays(new Date(), courseMarket.getValidDays());

            if (courseUserLearn!=null){
                //买过课程当前是否过期
                if(new Date().before(courseUserLearn.getEndTime())){//没有过期
                    date= DateUtils.addDays(courseUserLearn.getEndTime(),courseMarket.getValidDays());
                }
                courseUserLearn.setEndTime(date);
                courseUserLearn.setState(0);
                updateById(courseUserLearn);
            }else {
                courseUserLearn= new CourseUserLearn();
                courseUserLearn.setLoginId(userId);
                courseUserLearn.setStartTime(new Date());
                //end_time
                courseUserLearn.setEndTime(date);
                courseUserLearn.setState(0);
                courseUserLearn.setCourseId(Long.valueOf(id));
                courseUserLearn.setCourseOrderNo(payResultVo.getOrderSn());
                courseUserLearn.setCreateTime(new Date());
                insert(courseUserLearn);
            }
        }

    }
}
